package dk.itu.KrakMap;

import java.util.*;

/**
 * Keep a specific route, represented by roads on the map.
 *
 * @author ITU-KF04-2011, Group 11
 */
public class Route {

    private final List<KrakEdge> edges;
    private final KrakNode startNode;
    private final KrakNode endNode;
    private final HashSet<TravelType> travellerProfile;

    /**
     * get the startpoint of the route
     * @return the startNode of the route as a KrakNode
     */
    public KrakNode getStartNode() {
        return startNode;
    }

    /**
     * get the endnode of the route
     * @return the endNode of the route as a KrakNode
     */
    public KrakNode getEndNode() {
        return endNode;
    }

    /**
     * check if a node is on the route.
     * @param n the KrakNode to search for
     * @return true if an edge on the route has the given node as start- or endpoint.
     */
    public boolean goesThroughNode(KrakNode n) {
        for (KrakEdge e : edges) {
            if (e.v1 == n || e.v2 == n) {
                return true;
            }
        }
        return false;
    }

    /**
     * determines for each of the edges in the route whether the distance
     * of a given KrakNode to one of that edge's ends is smaller than a given threshold.
     * @param n KrakNode from which to calculate distance.
     * @param threshold double, the limit of the threshold.
     * @return true if node-point distance < threshold.
     */
    public boolean goesNearNode(KrakNode n, double threshold) {
        for (KrakEdge e : edges) {
            if (e.v1.getMapPoint().dist(n.getMapPoint()) < threshold
                    || e.v2.getMapPoint().dist(n.getMapPoint()) < threshold) {
                return true;
            }
        }
        return false;
    }

    /**
     * Create a Route, from a list of edges with default travelType:
     * TravelType.CAR.
     * @param edgeList List of edges for the Route.
     */
    public Route(List<KrakEdge> edges, KrakNode start, KrakNode end, HashSet<TravelType> travellerProfile) {
        this.edges = edges;
        this.startNode = start;
        this.endNode = end;
        this.travellerProfile = travellerProfile;
    }

    /**
     * get the travellerProfile
     * @return the set containing the chosen traveltype(s).
     */
    public HashSet<TravelType> getTravellerProfile() {
        return travellerProfile;
    }

    /**
     * get all edges in the route.
     * @return edges in the route as an Iterable.
     */
    public List<KrakEdge> getRoads() {
        return edges;
    }

    /**
     * get the length of the route.
     * @return the accumulated length of all the edges in the route as a double.
     */
    public double getLength() {
        double result = 0.0;
        for (KrakEdge e : edges) {
            if (e != null) {
                result += e.getLength();
            }
        }
        return result;
    }
}
